package com.thti.mecool.client.base;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

import com.thti.mecool.client.command.ManageCommand;
import com.thti.mecool.client.protocol.MessageInfo;
import com.thti.mecool.client.protocol.user.User;
import com.thti.mecool.client.util.Util;

public class BaseActivity extends Activity {

	static Activity mActivity;
	static Context context;
	SharedPreferences save_LoginInfo;
	public static boolean IsSyncEcho = true;
	private static MessageInfo currentMesg;
	public static ProgressDialog menuDialog;
	private static HashSet<String> DataNotify = new HashSet<String>();
	@SuppressWarnings("rawtypes")
	private static HashMap<Class, Object> DataList = new HashMap<Class, Object>();
	private static String failName;
	public static String getFailName() {
		return failName;
	}
	public static void setFailName(String failName) {
		BaseActivity.failName = failName;
	}
	public void setDataStat(HashMap<String,String> dataStat){
		BaseApplication baseApplication=(BaseApplication) context.getApplicationContext();
		baseApplication.setDataStat(dataStat);
	}
	public HashMap<String, String> getDataStat(){
		BaseApplication baseApplication=(BaseApplication) context.getApplicationContext();
		return baseApplication.getDataStat();
	}
	public static boolean isAutoLoad(){
		BaseApplication baseApplication=(BaseApplication) context.getApplicationContext();
		return baseApplication.isAutoLoad();
	}
	public static boolean ContainsDataList(Class DataClass) {
		if (DataList != null) {
			if (DataList.get(DataClass) != null) {
				return true;
			}
		}
		return false;
	}

	public BaseActivity() {
		System.out.println("Command!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		ManageCommand c = new ManageCommand();
		context = this;
		if (Cache.getmContext() == null) {
			Cache.setmContext(this);
		}
	}

	public void setLocate(double latitude, double longitude) {

	}

	public static Object getData(Class curClass) {
		return DataList.get(curClass);
	}

	public static void removeData(Class curClass) {
		DataList.remove(curClass);
	}

	public static void setData(Object object) {
		DataList.put(object.getClass(), object);
	}

	public static boolean getDataFlag(String DataFlag) {
		return DataNotify.contains(DataFlag);
	}

	public static void removeDataFlag(String DataFlag) {
		DataNotify.remove(DataFlag);
	}

	public static void setDataFlag(String DataFlag) {
		DataNotify.add(DataFlag);
	}

	public static MessageInfo getCurrentMesg() {
		return currentMesg;
	}

	public static void setCurrentMesg(MessageInfo currentMesg) {
		BaseActivity.currentMesg = currentMesg;
	}

	public void noTitle() {
		context = getApplicationContext();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(1);
	}

	/**
	 * 保存登录用户
	 * 
	 * @param user
	 */
	public static void setCurrentUser(User user) {
			BaseApplication application = (BaseApplication) context.getApplicationContext(); 
			application.setUser(user);
	}

	/**
	 * 获取应经登录用户
	 * 
	 * @return
	 */
	public static User getCurrentUser() {
		BaseApplication application = (BaseApplication) context.getApplicationContext();
		return application.getUser();
	}

	/**
	 * 退出程序
	 */
	public static void exitApp() {
		List<Activity> list = Cache.list;
		for (Activity activity : list) {
			activity.finish();
		}
		if (null != mActivity) {
			((ActivityManager) mActivity
					.getSystemService(Context.ACTIVITY_SERVICE))
					.restartPackage(mActivity.getPackageName());
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mActivity.startActivity(intent);
		}
	}

	public void dialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定要注销用户吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				setResult(RESULT_OK);// 确定按钮事件
				save_LoginInfo = getSharedPreferences("LoginPreferences", 0);
				Editor save_editor = save_LoginInfo.edit();
				save_editor.putString("userName", "null");
				save_editor.putString("passWord", "null");
				save_editor.putBoolean("autoLogin", false);
				save_editor.commit();
				exitApp();
				System.exit(0);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

			}
		});
		builder.create().show();
	}

	public void aa() {

	}

	public static Handler serverErrorHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (null != menuDialog) {
				menuDialog.dismiss();
			}
			if (!Thread.currentThread().isInterrupted()) {
				switch (msg.what) {
				case 1101:
					Util.sendToast(context, "下载地址无效");
					break;
				default:
					Util.sendToast(context, "服务器请求失败,请重试！");
					break;
				}
			}
			super.handleMessage(msg);
		}

	};

}
